﻿using BluetoothLE_Net8;
using InTheHand.Bluetooth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BluetoothLE_Utility
{
	public class BluetoothLE_Net8Utility
	{
		//public BluetoothDevice? selectedDevice { get; set; }		// 当前选中的设备
        //public GattService selectedService { get; set; }        // 当前选中的服务
		public GattCharacteristic selectedWriteCharacteristic { get; set; }
		public GattCharacteristic selectedNotifyCharacteristic { get; set; }	
		public List<BluetoothDevice> bluetoothDevices { get; set; }
		public event EventHandler<RecvEventArgs> ReceivedDataEvent;

		/// <summary>
		/// 异步扫描设备
		/// </summary>
		/// <returns></returns>
		public async Task ScanForDevicesAsync()
		{
			try
			{
				bluetoothDevices = new List<BluetoothDevice>();
				IReadOnlyCollection<BluetoothDevice> devices = await Bluetooth.ScanForDevicesAsync();    //  异步扫描设备
				foreach (BluetoothDevice device in devices)
				{
					bluetoothDevices.Add(device);
				}
			}
			catch (Exception)
			{
				throw;
			}
			
		}

		/// <summary>
		/// 清空发送通道
		/// </summary>
		/// <returns></returns>
		public void StopWriteCharacteristicAsync()
		{
			if (selectedWriteCharacteristic != null)
			{
				selectedWriteCharacteristic = null;
			}
		}


		/// <summary>
		/// 停止通知
		/// </summary>
		/// <returns></returns>
		public async Task StopNotificationsAsync()
		{
			if (selectedNotifyCharacteristic != null)
			{
				selectedNotifyCharacteristic.CharacteristicValueChanged -= CharacteristicRx;
				await selectedNotifyCharacteristic.StopNotificationsAsync();
				selectedNotifyCharacteristic = null;
			}
		}

		/// <summary>
		/// 开启通知
		/// </summary>
		/// <returns></returns>
		public async Task StartNotificationsAsync()
		{
			if (selectedNotifyCharacteristic != null)
			{
				await selectedNotifyCharacteristic.StartNotificationsAsync();
				//selectedNotifyCharacteristic.CharacteristicValueChanged += (s, e) =>
				//{
				//	var data = e.Value;  // byte[]
				//	if (ReceivedDataEvent != null)
				//	{
				//		ReceivedDataEvent.Invoke(this, new RecvEventArgs(data, data.Length));
				//	}
				//};
				selectedNotifyCharacteristic.CharacteristicValueChanged += CharacteristicRx;
			}
		}

		
		public void Clear()
		{
            if(bluetoothDevices != null) bluetoothDevices.Clear();
			//selectedDevice = null;
		}

		/// <summary>
		/// 发送数据
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public async Task WriteValueWithoutResponseAsync(byte[] data)
        {
			if(selectedWriteCharacteristic != null)
			{
				await selectedWriteCharacteristic.WriteValueWithoutResponseAsync(data);
			}

			//if (ReceivedDataEvent != null)
			//{
			//	ReceivedDataEvent.Invoke(this, new RecvEventArgs(data, data.Length));
			//}
		}

		/// <summary>
		/// 接收到数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CharacteristicRx(object? sender, GattCharacteristicValueChangedEventArgs e)
        {
			try
			{
				var data = e.Value;
				if (ReceivedDataEvent != null)
				{
					ReceivedDataEvent.Invoke(this, new RecvEventArgs(data, data.Length));
				}
			}
			catch (Exception)
			{

				throw;
			}
		}
	}
}
